package com.cykj.util.excel;


import com.cykj.pojo.ExcelUser;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExcelImportUtil {
    //读取工具类
    public static List<ExcelUser> readData(String filePath) {
        List<ExcelUser> userInfo = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(new File(filePath));
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0); // 假设我们要读取第一个工作表

            // 假设我们要从第二行开始读取数据（第一行通常是标题行）
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {

                Row row = sheet.getRow(rowNum);
                if (row != null) { // 避免空行
                    // 假设我们只关心第一列和第二列的数据
                    Cell name = row.getCell(0);
                    Cell sex = row.getCell(1);
                    Cell age = row.getCell(2);
                    Cell cardID = row.getCell(3);
                    Cell money = row.getCell(4);
                    Cell weight = row.getCell(5);
                    Cell height = row.getCell(6);
                    Cell phone = row.getCell(7);

                    String value1 = name.getStringCellValue(); // 假设这是字符串类型
                    String value2 = sex.getStringCellValue(); // 假设这是字符串类型
                    age.setCellType(CellType.STRING);
                    int value3 = Integer.parseInt(age.getStringCellValue());
                    cardID.setCellType(CellType.STRING);
                    String value4 = cardID.getStringCellValue(); // 假设这是字符串类型
                    money.setCellType(CellType.STRING);
                    int value5 = Integer.parseInt(money.getStringCellValue());
                    weight.setCellType(CellType.STRING);
                    int value6 = Integer.parseInt(weight.getStringCellValue());
                    height.setCellType(CellType.STRING);
                    int value7 = Integer.parseInt(height.getStringCellValue());
                    phone.setCellType(CellType.STRING);
                    String value8 = phone.getStringCellValue(); // 假设这是字符串类型

                    ExcelUser excelUser = new ExcelUser(value1,value2,value3,value4,value5,value6,value7,value8);
                    userInfo.add(excelUser);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return userInfo;
    }
}
